package com.jshan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshan.common.annotation.OperationLog;
import com.jshan.base.entity.SysServer;
import com.jshan.service.SysServerService;
import com.jshan.base.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

@Tag(name = "服务器管理接口")
@RestController
@RequestMapping("/server")
@RequiredArgsConstructor
public class SysServerController {
    private final SysServerService serverService;

    // @SaCheckPermission("server:list")
    // @OperationLog("查询服务器列表")
    // @GetMapping("/list")
    // public Result<List<SysServer>> list() {
    // return Result.ok(serverService.listAll());
    // }

    @SaCheckPermission("server:list")
    @OperationLog("查询服务器分页列表")
    @Operation(summary = "服务器分页列表")
    @GetMapping("/page")
    public Result<IPage<SysServer>> getServerPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String serverName) {
        return Result.ok(serverService.getServerPage(new Page<>(pageNum, pageSize), serverName));
    }

    @SaCheckPermission("server:add")
    @OperationLog("新增服务器")
    @PostMapping
    public Result<Void> addServer(@RequestBody SysServer server) {
        if (serverService.saveServer(server)) {
            return Result.ok(200, "新增服务器成功");
        } else {
            return Result.fail(500, "新增服务器失败");
        }
    }

    @SaCheckPermission("server:edit")
    @OperationLog("修改服务器")
    @PutMapping
    public Result<Void> updateServer(@RequestBody SysServer server) {
        if (serverService.updateServer(server)) {
            return Result.ok(200, "更新服务器成功");
        } else {
            return Result.fail(500, "更新服务器失败");
        }
    }

    @SaCheckPermission("server:delete")
    @OperationLog("删除服务器")
    @DeleteMapping("/{id}")
    public Result<Void> deleteServer(@PathVariable Long id) {
        if (serverService.deleteServer(id)) {
            return Result.ok(200, "删除服务器成功");
        } else {
            return Result.fail(500, "删除服务器失败");
        }
    }
}
